package cn.cmxds.store.presenter.Impl;

import android.view.KeyEvent;
import android.view.View;
import android.widget.AdapterView;
import android.widget.EditText;
import android.widget.GridView;
import android.widget.TextView;

import com.daimajia.slider.library.SliderTypes.TextSliderView;
import com.squareup.okhttp.Request;
import com.squareup.okhttp.Response;
import com.sunfusheng.marqueeview.MarqueeView;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;

import java.util.ArrayList;
import java.util.List;

import cn.cmxds.store.R;
import cn.cmxds.store.adapter.HomeCatgoryAdapter;
import cn.cmxds.store.bean.Banner;
import cn.cmxds.store.bean.Campaign;
import cn.cmxds.store.bean.HomeCampaign;
import cn.cmxds.store.http.BaseCallback;
import cn.cmxds.store.http.OkHttpHelper;
import cn.cmxds.store.http.ResultCallBack;
import cn.cmxds.store.model.IHomeModel;
import cn.cmxds.store.model.Impl.HomeModel;
import cn.cmxds.store.presenter.IHomePresenter;
import cn.cmxds.store.view.IHomeView;
import okhttp3.Call;


/**
 * 主页实现类
 */
public class HomePresenter implements IHomePresenter{
    //存放轮播图片Id
    private ArrayList<Integer> imgs;
    //存放中间小控件的id
    private Integer[] sortImgs;
    //存放中间的文字
    private String[] sortStrs;
   //    存放轮播广告数据
   private List<Banner> slList;
    private GridView gridViewSort;
    private MarqueeView marqueeViewTop;
    private EditText editText;
    private IHomeView view;
    private IHomeModel model;
    private TextSliderView mSliderView;
    HomeCatgoryAdapter HomeCatgoryAdapter;
//    存放RecyclerView数据
    private List<HomeCampaign> rcList;
    private List<Campaign> rcList1;
    private List<Campaign> rcList2;
    private List<Campaign> rcList3;

    /**
     * 构造方法取得view实例
     * @param view
     */
    public HomePresenter(IHomeView view) {
        this.view = view;
        view.setPresenter(this);
    }

    /**
     * 设置数据模型
     */
    @Override
    public void setModel() {
        model=new HomeModel();
    }



    @Override
    public void initData() {

        //获取中间显示各个小控件的GridView
        gridViewSort = view.getGridViewSort();
        //获取显示资讯头条的控件
        marqueeViewTop = view.getMarqueeViewTop();
        //获取搜索框
        editText = view.getmFragHomeEtSearch();

        mSliderView = view.getTextSliderView();

        //初始化数据源

        setModel();

//      轮播动画数据
       mInitSlider();
//       首页按键
       mInitGrid();
//       首页滚动资讯
       mInitViewTop();
       //初始化监听
        initRecyclerView();
        HomeCatgoryAdapter = view.getmAdatper();
        initEvent();
    }

    private void initRecyclerView() {
//
       model.initRecyclerView(new ResultCallBack<List<HomeCampaign>>() {
           @Override
           public void onResponse(List<HomeCampaign> homeCampaigns) {
               HomePresenter.this.view.initRecyclerView(homeCampaigns);

           }
       });


    }

    /**
     * 首页滚动资讯
     */
private void mInitViewTop(){
    model.initViewTop(new ResultCallBack<List<String>>() {
        @Override
        public void onResponse(List<String> info) {
            //启动滚动
            HomePresenter.this.view.initViewTop(info);
        }
    });

    }
    /**
     * 首页按钮
     */
    private void mInitGrid() {
        sortImgs = new Integer[]{R.drawable.frag_home_sort_tianmao, R.drawable.frag_home_sort_juhuasuan,
                R.drawable.frag_home_sort_jinkou, R.drawable.frag_home_sort_waimai, R.drawable.frag_home_sort_market,
                R.drawable.frag_home_sort_chongzhi, R.drawable.frag_home_sort_travel, R.drawable.frag_home_sort_tao,
                R.drawable.frag_home_sort_daojia, R.drawable.frag_home_sort_type};
        sortStrs = new String[]{"天猫", "聚划算", "天猫国际", "外卖", "天猫超市", "充值中心", "阿里旅行",
                "领金币", "到家", "分类"};
//显示
        HomePresenter.this.view.initGridView(sortImgs, sortStrs, R.layout.frag_home_gv_sort_item);
    }

    /**
     * 首页轮播动画
     */
    public void mInitSlider(){
        model.initSlider(new ResultCallBack<List<Banner>>() {
            @Override
            public void onResponse(List<Banner> response) {
                HomePresenter.this.view.initSlider(response);
            }
        });
    }

    /**
     * 初始化监听事件
     */
    private void initEvent() {
        //为中间小控件GridView设置监听事件
        gridViewSort.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                switch (position) {
                    case 0:
                      HomePresenter.this.view.jumpActivity(0, "");
                        break;
                    case 1:
                        HomePresenter.this.view.jumpActivity(1, "");
                        break;
                    case 2:
                        HomePresenter.this.view.jumpActivity(2, "");
                        break;
                    case 3:
                        HomePresenter.this.view.jumpActivity(3, "");
                        break;
                    case 4:
                        HomePresenter.this.view.jumpActivity(4, "");
                        break;
                    case 5:
                        HomePresenter.this.view.jumpActivity(5, "");
                        break;
                    case 6:
                        HomePresenter.this.view.jumpActivity(6, "");
                        break;
                    case 7:
                        HomePresenter.this.view.jumpActivity(7, "");
                        break;
                    case 8://跳转到webview,暂时不传递数据，仅进行数据测试
                        HomePresenter.this.view.jumpActivity(8, "");
                        break;
                    case 9://分类
                        HomePresenter.this.view.jumpActivity(9, "");
                        break;
                }
            }
        });

        //为搜索按钮设置事件
        editText.setOnEditorActionListener(new TextView.OnEditorActionListener() {

            @Override
            public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
                //点击回车处理事件
                //根据输入内容查询类型id
                queryDatasFromServer(editText.getText().toString());

                //跳转
                HomePresenter.this.view.jumpActivity(10, "");
                return false;
            }
        });
//        首页资讯点击事件
        marqueeViewTop.setOnItemClickListener(new MarqueeView.OnItemClickListener() {
            @Override
            public void onItemClick(final int position, TextView textView) {
                model.getViewTopUrl(new ResultCallBack<List<String>>() {
                    @Override
                    public void onResponse(List<String> response) {
                        HomePresenter.this.view.showMsg(response.get(position));
                    }
                });
            }
        });
    }



        /**
         * 根据输入内容查询类型id
         *
         * @param inputText 输入的内容
         */
        private void queryDatasFromServer(String inputText) {
            model.initData(inputText);

        }

    }


